﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OtomatSearching.Engine;

namespace OtomatSearching
{
    public partial class frmChooseFolder : Form
    {
        public frmChooseFolder()
        {
            InitializeComponent();
            otSearch = new OtomatSearch();
        }

        #region Fields
        private string[] WordFilesPath = { };

        public DataTable TblKeywords { get; set; }

        public DataTable TblResult { get; set; }

        private readonly OtomatSearch otSearch;
        #endregion

        public void DataBind()
        {
            TblResult = new DataTable();
            List<string> fileContents = new List<string>();

            TblResult.Columns.Add("Tên file");
            TblResult.Columns.Add("Tổng số từ");

            foreach (DataRow rw in TblKeywords.Rows)
            {
                TblResult.Columns.Add(rw[0].ToString());
            }

            dgridKeywordTable.DataSource = TblResult;
        }

        private void frmChooseFolder_Load(object sender, EventArgs e)
        {

        }

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {

        }

        private void txtSelectFolder_TextChanged(object sender, EventArgs e)
        {

        }

        private void lblFolderInfo_Click(object sender, EventArgs e)
        {

        }

        private void btnSelectFolder_Click_1(object sender, EventArgs e)
        {
            var fsd = new FolderSelectDialog();
            fsd.Title = "Select a folder";
            fsd.InitialDirectory = @"c:\";

            var path = "";
            if (fsd.ShowDialog(IntPtr.Zero))
            {
                path = fsd.FileName;
                WordFilesPath = Directory.GetFiles(path, "*.doc?");
                // Format richtextbox
                this.txtSelectFolder.Text = path;
                this.lblFolderInfo.Text = string.Format("Có {0} file word trong folder bạn chọn.", WordFilesPath.Count());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSelectFolder.Text == "")
                {
                    MessageBox.Show("Bạn chưa chọn folder!", "Cảnh báo", MessageBoxButtons.OK);
                    return;
                }

                OpenFolderAndProcess(txtSelectFolder.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                FileHelper.Export2Csv(dgridKeywordTable.DataSource as DataTable);
                MessageBox.Show("Export thành công!", "Thông báo", MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Out to a datatable and binding it to gridview
        /// </summary>
        /// <param name="path"></param>
        private void OpenFolderAndProcess(string path)
        {

            foreach (var file in WordFilesPath)
            {
                var text = "";
                try
                {
                    text = FileHelper.ReadWordDocument(file);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Có vấn đề khi mở file word {0}. Hãy thông báo với chúng tôi để được hỗ trợ. \r\nBấm OK để tiếp tục chương trình.", Path.GetFileName(file)), "Error", MessageBoxButtons.OK);
                }
                DataRow row = TblResult.NewRow();
                row[0] = Path.GetFileName(file);
                row[1] = otSearch.GetWordNumber(text); // Word total
                for (int i = 0; i < TblKeywords.Rows.Count; i++)
                {
                    row[i + 2] = otSearch.Search(text, TblKeywords.Rows[i][0].ToString(), false).Count;
                }

                TblResult.Rows.Add(row);

            }

            //foreach (var file in WordFilesPath)
            //{
            //    result.Columns.Add(Path.GetFileName(file));
            //    fileContents.Add(FileHelper.ReadWordDocument(file));
            //}

            //foreach (DataRow dr in TblKeywords.Rows)
            //{
            //    DataRow row = result.NewRow();
            //    row[0] = dr[0];
            //    for (int i = 0; i < fileContents.Count; i++)
            //    {
            //        row[i + 1] = otSearch.Search(fileContents[i], dr[0].ToString(), false).Count;
            //    }

            //    result.Rows.Add(row);
            //}

            dgridKeywordTable.DataSource = TblResult;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
